package com.github.lee.mvvm.core.ui.web

import androidx.databinding.ObservableField
import com.github.lee.mvvm.core.databinding.LayoutBaseWebviewBinding
import com.github.lee.mvvm.core.ui.BaseActivity

open class WebViewObservableData {

//    val title = ObservableField("")
//    val url = ObservableField("")

}

abstract class BaseWebViewActivity<VM : BaseWebViewVM> :
    BaseActivity<LayoutBaseWebviewBinding, VM>() {

    val loadProgress = ObservableField(0)

    private val webHandler by lazy {
        WebHandler(this)
    }
    private val wod = WebViewObservableData()

    fun getWebViewObservableData(): WebViewObservableData {
        return wod
    }


    override fun onCreateSuccess(): LayoutBaseWebviewBinding =
        LayoutBaseWebviewBinding.inflate(layoutInflater)

    //===Desc:================================================================================

    override fun onSetViewData() {
        super.onSetViewData()
        pageVB?.apply {
            wvContent.webViewClient =
                onCreateWebViewClient(webHandler) ?: CustomizeWebViewClient(webHandler)
            wvContent.webChromeClient =
                onCreateWebChromeClient(webHandler) ?: CustomizeWebChromeClient(webHandler)
            wvContent.loadUrl(getLoadUrl())
            progress = loadProgress
        }

    }

    //===Desc:================================================================================

    protected abstract fun getLoadUrl(): String


    protected open fun onCreateWebViewClient(webHandler: WebHandler): CustomizeWebViewClient? = null
    protected open fun onCreateWebChromeClient(webHandler: WebHandler): CustomizeWebChromeClient? =
        null

}